package com.jungle.pojo;

import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * 基本实体
 */
@Data
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(
            generator = "system-uuid"
    )
    @GenericGenerator(
            name = "system-uuid",
            strategy = "uuid"
    )
    @Comment(value = "id")
    private String id;

    //创建时间
    @Comment("创建时间")
    @Column(name = "create_time")
    private LocalDateTime createTime;

    //修改时间
    @Comment("修改时间")
    @Column(name = "update_time")
    private LocalDateTime updateTime;


    @Comment("是否删除")
    @Column(name = "deleted",columnDefinition = "bit default 0")
    @TableLogic(value = "false", delval = "true")
    private Boolean deleted = false;

    @PreRemove
    public void delete() {
        this.deleted = true;
    }

    /**
     * 保存数据之前的操作
     */
    @PrePersist
    public void onCreate() {
        createTime = createTime == null ? LocalDateTime.now() : createTime;
        updateTime = updateTime == null ? LocalDateTime.now() : updateTime;
//        try {
//            if (StpUtil.isLogin()) {
//                LoginUser loginUser = LoginHelper.getLoginUser();
//                if (loginUser != null) {
//                    createBy = loginUser.getUserId();
//                    updateBy = loginUser.getUserId();
//                }
//            }
//        } catch (Exception e) {
//        }
//        if (status == null) {
//            status = 0;
//        }
    }
//更新之前的操作
    @PreUpdate
    public void onUpdate() {
        updateTime = LocalDateTime.now();
//        try {
//            LoginUser loginUser = LoginHelper.getLoginUser();
//            if (loginUser != null) {
//                updateBy = loginUser.getUserId();
//            }
//        } catch (Exception e) {
//
//        }
    }

}
